#ifndef SOCKETWORK_H
#define SOCKETWORK_H

#include <QObject>
#include <QTcpSocket>
#include <QPixmap>

#define MSG_HEADER_LENGTH 4
#define MSG_TOTAL_LENGTH 4
#define MSG_TYPE_LENGTH 4
#define MSG_STREAM_LENGTH 4

enum ServerMsgType
{
    ServerSendCommand = 200,
    ServerSendFile,

    ServerSendShakeHand = 250,
    ServerReceiveShakeHand,
};

enum ClientMsgType
{
    ClientSendHeartbeat = 0,
    ClientReceiveShakeHand = 50,
    ClientSendShakeHand = 51,
    ClientSendCommand = 100,
    ClientSendIntric,
    ClientSendPreview,
};



class SocketWork : public QObject
{
    Q_OBJECT
public:
    explicit SocketWork(qintptr desc_ptr, QObject *parent = nullptr);

signals:
    void update_textBrowser_signal(QString message);

    void init_navigation_widget_signal();

    void navigation_load_data_signal();

    void close_navigation_widget_signal();

    void work_disconnect_signal(qintptr desc_ptr);

public slots:

    void send_Message_slot(qintptr desc_ptr, QString meg);

    void send_file_slot(qintptr desc_ptr, QString path);

    void send_pic_slot(qintptr desc_ptr, QString path);

    void send_data_slot(qintptr desc_ptr, const QByteArray &data);

    void receive_data_slot();

    void disconnect_socket_slot(qintptr desc_ptr);

    void Update_IP_slot(QString ip);

signals:
    void disconnect_socket_signal(qintptr desc_ptr);

    void receive_data_signal(qintptr desc_ptr, const QByteArray &data);

    void preview_image_signal(QPixmap img);

private:
    int unpack_receive_data();

    void handle_command_message(const QByteArray &message);

    void handle_preview_message(const QByteArray &bytes);
private:
    QTcpSocket *m_socket;
    qintptr m_desc;

    QByteArray receive_bytes;
    QString localIP;
};

#endif // SOCKETWORK_H
